/**
 * 作者:李杰
 * email: maillijie@gmail.com
 */
package com.javainfo.service;

import java.util.ArrayList;
import java.util.List;

import com.javainfo.dao.inf.BaseDaoIf;
import com.javainfo.dao.inf.UserDaoIf;
import com.javainfo.entity.User;
import com.javainfo.exception.AppException;
import com.javainfo.service.inf.UserServiceIf;
import com.javainfo.simpledto.UserDto;

public class UserService extends BaseService<User> implements UserServiceIf{

	/**  userDao */
	public UserDaoIf userDao;

	/**
	 * @return
	 */
	public User checkUser(String userAccount, String passwd) {
		User user = new User();
		user.setPasswd(passwd);
		user.setUserAccount(userAccount);
		List<User> userList = userDao.getByTemp(user);
		if (userList.size() != 1) {
			return null;
		} else {
			return userList.get(0);
		}
	}

	@Override
	public BaseDaoIf<User> getBaseDao() {
		return userDao;
	}

	public UserDaoIf getUserDao() {
		return userDao;
	}

	public void setUserDao(UserDaoIf userDao) {
		this.userDao = userDao;
	}

	public List<UserDto> getAllUser() {
		List<User> userList = getAll();
		List<UserDto> users = new ArrayList<UserDto>();
		for(User user : userList){
			UserDto userDto = new UserDto();
			userDto.setId(String.valueOf(user.getId()));
			userDto.setUserAccount(user.getUserAccount());
			users.add(userDto);
		}
		return users;
	}
	
	@Override
	public void delete(User obj) {
		obj = getById(obj.getId());
		if (obj.getArticles() != null && obj.getArticles().size() > 0) {
			throw new AppException("用户有关联的文章，不能删除!");
		} else {
			super.delete(obj);
		}
	}

	/* (non-Javadoc)
	 * @see com.javainfo.service.inf.UserServiceIf#getUserAccountListByStart(java.lang.String)
	 */
	public List<String> getUserAccountListByStart(String start) {
		List<String> userAccountList = new ArrayList<String>();
		List<User> userList = userDao.getUserListByAccountStart(start);
		for(User user : userList){
			userAccountList.add(user.getUserAccount());
		}
		return userAccountList;
	}
}
